/**
 * 
 */
package com.fish.service.impl.user;

import java.awt.Menu;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.ListIterator;
import java.util.Map;

import com.fish.common.constant.QueryPage;
import com.fish.service.user.MenusService;
import com.fish.user.dao.MenusDAO;
import com.fish.user.po.Menus;
import com.fish.user.po.TreeBean;
import com.fish.user.po.User;

/**
 * 
 */
public class MenusServiceImpl implements MenusService {
	private MenusDAO menusDAO;

	public void setMenusDAO(MenusDAO menusDAO) {
		this.menusDAO = menusDAO;
	}

	@Override
	public Menus queryMenusByPrimaryKey(String menusId) {
		return menusDAO.queryMenusByPrimaryKey(menusId);
	}

	@Override
	public void addMenus(Menus menus) {
		menusDAO.addMenus(menus);
	}

	@Override
	public void updateMenus(Menus menus) {
		menusDAO.updateMenus(menus);
	}

	@Override
	public List<Menus> queryMenusByObject(Menus menus) {
		return menusDAO.queryMenusByObject(menus);
	}
	
	@Override
	public Long queryMenusCountByObject(Menus menus) {
		return this.menusDAO.queryMenusCountByObject(menus);
	}

	@Override
	public List<Menus> queryMenusByObject(Menus menus, QueryPage queryPage) {
		return this.menusDAO.queryMenusByObject(menus,queryPage);
	}

	@Override
	/**
	 * 写一个层级树算法
	 * 
	 */
	public TreeBean<Menus> queryMenusByUser(User userDB) {
		List<Menus> menuList =  this.menusDAO.queryMenusByUser(userDB);		
		return getMenuTree(menuList);
	}

	/**
	 * 构造一个菜单的树
	 * **/
	private TreeBean<Menus> getMenuTree(List<Menus> menuList) {
		TreeBean<Menus> userMenuTree = new TreeBean<Menus>();
		if (menuList.size() > 0) {
			List<TreeBean> firstMenuList = new ArrayList<TreeBean>();
			for (int i = 0; i < menuList.size(); i++) {
				if ("FIRST".equals(menuList.get(i).getTypes())) {
					TreeBean<Menus> firstMenu = new TreeBean<Menus>();
					firstMenu.setNode(menuList.get(i));
					firstMenu.setSort(menuList.get(i).getRate());
					firstMenuList.add(firstMenu);
				}
			}
			Collections.sort(firstMenuList, new TreeBean.comparatorChinaTree());
			userMenuTree.setSubNode(firstMenuList);
			if (firstMenuList.size() > 0) {
				for (int i = 0; i < firstMenuList.size(); i++) {
					List<TreeBean> secondMenuList = new ArrayList<TreeBean>();
					for (int j = 0; j < menuList.size(); j++) {
						Menus menus = (Menus) firstMenuList.get(i).getNode();
						if (menuList.get(j).getParentId().equals(menus.getMenuId())) {
							TreeBean<Menus> secondMenu = new TreeBean<Menus>();
							secondMenu.setNode(menuList.get(j));
							secondMenu.setSort(menuList.get(j).getRate());
							secondMenuList.add(secondMenu);
						}

					}
					Collections.sort(secondMenuList, new TreeBean.comparatorChinaTree());
					firstMenuList.get(i).setSubNode(secondMenuList);
				}
			}
		}
		return userMenuTree;
	}
  
}
